﻿using ProductSystem.BLL.Common;
using ProductSystem.Model;
using Fundway.Common.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace ProductSystem.BLL
{
    public partial class Base_CodeBll
    {
        static string key = "BaseCode";

        /// <summary>
        /// 添加/更新缓存
        /// </summary>
        public static void Cache()
        {
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                CacheRepository.AddOrReplace(key, dbContext.Base_Codes.Where(m => m.Status != 0).ToList(), CacheExpirationTypes.RelativelyStable);
            }
        }

        /// <summary>
        /// 检查缓存，若缓存为空则添加缓存
        /// </summary>
        public static void CheckCache()
        {
            if (CacheRepository.Get(key, true) == null)
            {
                Cache();
            }
        }

        /// <summary>
        /// 获取BaseCodeList
        /// </summary>
        /// <returns></returns>
        public static List<Base_Code> GetList()
        {
            CheckCache();
            return CacheRepository.Get(key, true) as List<Base_Code>;
        }

        /// <summary>
        /// 根据TypeCode获取基础码数据
        /// </summary>
        /// <param name="typeCode">typeCode</param>
        /// <returns></returns>
        public static List<Base_Code> GetListByTypeCode(string typeCode)
        {
            List<Base_Code> list = GetList();
            return list.Where(t => t.Type_Code == typeCode).ToList();
        }

        /// <summary>
        /// 根据Data_Key或者Data_Value模糊查询BaseCode
        /// </summary>
        /// <param name="baseCode"></param>
        /// <returns></returns>
        public static List<Base_Code> GetList(string baseCode, PageInfo pageInfo)
        {
            List<Base_Code> list = GetList();
            if (!string.IsNullOrWhiteSpace(baseCode))
            {
                list = list.Where(t => t.Data_Key.Contains(baseCode) || t.Data_Value.Contains(baseCode)).Page<Base_Code>(pageInfo);
            }
            return list.OrderByDescending(t => t.ID).ToList();
        }

        /// <summary>
        /// 判断是否存在该记录
        /// </summary>
        /// <param name="model">数据元</param>
        /// <returns></returns>
        public static bool IsExist(Base_Code model)
        {
            bool flag = false;
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                Base_Code item = dbContext.Base_Codes.FirstOrDefault(o => o.Type_Code == model.Type_Code && o.Data_Key == model.Data_Key && o.ID != model.ID && o.Status != 0);
                if (item != null) flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 获取BaseCode下拉菜单
        /// </summary>
        /// <param name="typeCode">类型码</param>
        /// <returns></returns>
        public static SelectList GetSelectList(string typeCode)
        {
            List<Base_Code> list = GetListByTypeCode(typeCode);
            return new SelectList(list, "Data_Key", "Data_Value");
        }

        /// <summary>
        /// 获取BaseCode字典
        /// </summary>
        /// <param name="typeCode">类型码</param>
        /// <returns></returns>
        public static Dictionary<string, string> GetDictionary(string typeCode)
        {
            Dictionary<string, string> result = GetListByTypeCode(typeCode).ToDictionary(o => o.Data_Key, o => o.Data_Value);
            return result;
        }


        /// <summary>
        /// 根据Data_Key查询Base_Code
        /// </summary>
        /// <param name="dataKey">Data_Key</param>
        /// <returns></returns>
        public static Base_Code GetBaseCodeBy(string dataKey)
        {
            List<Base_Code> list = GetList();
            return list.FirstOrDefault(o => o.Data_Key == dataKey);
        }

        /// <summary>
        /// 根据ID查询BaseCode
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Base_Code GetData(int id)
        {
            List<Base_Code> list = GetList();
            return list.FirstOrDefault(t => t.ID == id);
        }

        /// <summary>
        /// 是否存用相同的码名称
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="id">用户ID</param>
        /// <returns>true/false</returns>
        public static bool ExistBaseCodeByDataValue(string TypeCode, string DataValue)
        {
            bool reStr = false;
            int total = 0;
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                total = dbContext.Base_Codes.Where(m => m.Type_Code == TypeCode && m.Data_Value == DataValue).Count();

                reStr = total == 0 ? false : true;
            }
            return reStr;
        }

        /// <summary>
        /// 是否存用相同的码名称
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="id">用户ID</param>
        /// <returns>true/false</returns>
        public static bool ExistBaseCodeByDataKey(int id, string TypeCode, string Data_Key)
        {
            bool reStr = false;
            int total = 0;
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                total = dbContext.Base_Codes.Where(m => m.Type_Code == TypeCode && m.Data_Key == Data_Key && m.ID != id).Count();
                reStr = total == 0 ? false : true;
            }
            return reStr;
        }
    }
}
